﻿using NoIdeaSelfHost.Codes;
using Topshelf;

namespace NoIdeaSelfHost
{
    internal class Program
    {
        const string DefaultServiceName = "NoIdea-SelfHost-Service";
        const string DefaultServiceDescription = "不知的Demo自托管服务";
        static void Main(string[] args)
        {
            using (var container = HostConfigurator.InitContainer())
            {
                HostFactory.New(x =>
                {
                    x.UseIocContainer();
                    x.UseAssemblyInfoForServiceInfo();
                    x.Service<Service>(s =>
                    {
                        s.ConstructUsing(serviceFactory => container.Resolve<Service>());
                        s.WhenStarted(async tc => await tc.StartAsync());
                        s.WhenStopped(async tc => await tc.StopAsync());
                    });
                    x.EnableServiceRecovery(r => r.RestartService(TimeSpan.FromSeconds(10)));
                    x.StartAutomatically();
                    x.RunAsLocalSystem();
                    string serviceName = DefaultServiceName;
                    string serviceDescription = DefaultServiceDescription;
                    x.SetDescription(serviceDescription);
                    x.SetDisplayName(serviceName);
                    x.SetServiceName(serviceName);
                    x.StartAutomaticallyDelayed();
                    x.SetStartTimeout(TimeSpan.FromMinutes(5));
                    x.SetStopTimeout(TimeSpan.FromMinutes(5));
                }).Run();
            }
        }
    }
}
